package com.sugon.quartz.listener;

import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.EverythingMatcher;
import org.quartz.impl.matchers.KeyMatcher;

import com.sugon.quartz.simpletrigger.SimpleJob;

/**
 * @desc:
 * @author: luoy
 * @date: 2021-12-09 16:59
 */
public class ListenerTest {


    public static void main(String[] args) throws SchedulerException {

        //创建一个job
        JobDetail job = JobBuilder.newJob(SimpleJob.class)
                .withIdentity("job", "group").build();

        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        //注册trigger并启动scheduler
        scheduler.scheduleJob( job,get1());

        //绑定job监听
        //1.全局绑定，所有的job在被调度的时候都会被监听
        //scheduler.getListenerManager().addJobListener(new MyJobListener(), EverythingMatcher.allJobs());
        //2.局部监听，用来监听指定的job
        scheduler.getListenerManager().addJobListener(new MyJobListener(),KeyMatcher
                //  //jobKey中的name参数和group参数对应的就是任务实例【JobDetail】的name和group
                .keyEquals(new JobKey("job","group")));

        //全局监听
        scheduler.getListenerManager().addTriggerListener(new MyTriggerListener(), EverythingMatcher.allTriggers());

        scheduler.start();

    }

    public  static SimpleTrigger get1(){
       //指定时间开始触发，不重复：
        SimpleTrigger trigger = (SimpleTrigger) newTrigger()
                .withIdentity("trigger1", "group1")
                .startAt(new Date())                     // some Date
                 .forJob("job", "group")                 // identify job with name, group strings
                .build();
        return trigger;
    }



}
